package edu.chalmers.trec;

import java.util.concurrent.ConcurrentHashMap;

import android.content.Intent;
import android.content.res.Configuration;
import android.nfc.NfcEvent;
import android.widget.Toast;
import edu.chalmers.targine.ARManager;
import edu.chalmers.targine.Arbject;
import edu.chalmers.targine.GameLayer;
import edu.chalmers.targine.SettingsActivity;
import edu.dhbw.andar.ARObject;
import edu.dhbw.andar.exceptions.AndARException;
import edu.dhbw.andar.util.AssetsFileUtil;
import edu.dhbw.andar.util.BaseFileUtil;

public class TREC extends ARManager {
	private Planet terra;
	private Planet luna;

	private int food = 0;
	private int energy = 0;
	private int oxygen = 0;
	private int population = 0;

	public static final String SETTINGS = "settings";
	public static final String PLANET = "planet";
	public static final String TERRA = "terra";
	public static final String LUNA = "luna";

	private PlanetArbject currentArbject = null;

//	private Text debugText;

	// private HashMap<String, Integer> initiatives = new HashMap<String,
	// Integer>();
	// private ArrayList<String> players = new ArrayList<String>();
	// private int nextPlayerIndex = 0;

	// private CardArbject resourceArbject = null;
	// private Button proceedButton, redButton, greenButton, blueButton;
	// private HandInterface handInterface;
	// private HandInterface plotInterface;
	// private CombatStats combatStats;
	private Button bSettings;

	@Override
	public void onStart() {
		super.onStart();
		
		bSettings = new Button("d-pad.png", 98, 98, SETTINGS);
    	((GameLayer)(cocosScene.getChildren().get(0))).addComponent(bSettings);

		terra = new Planet("Terra", 1000, 1000, 1000, 1000);
		luna = new Planet("Luna", 0, 0, 0, 0);

//		setSharedData("6c be 02 06 66 24 16 e0", PLANET, terra.packValues());
//		setSharedData("d6 db 86 30", PLANET, luna.packValues());
		
		setSharedData("ee 0a 6d b7", PLANET, terra.packValues());
		setSharedData("de cf 3d d2", PLANET, luna.packValues());

//		debugText = new Text(WINDOW_WIDTH / 2, 600, "");
//		((GameLayer) (cocosScene.getChildren().get(0))).addComponent(debugText);
	}

	protected void registerModels() {
		arbjects = new ConcurrentHashMap<String, Arbject>();

		try {
			BaseFileUtil fileUtil = new AssetsFileUtil(getResources()
					.getAssets());
			fileUtil.setBaseFolder("models/");

			if (fileUtil != null) {
				registerArbject("0c 10 ef 0f", "linda", "pattern1");
				registerArbject("ae e0 41 d2", "oleander", "pattern3");
				registerArbject("5e 6a 42 d2", "pokeylope", "pattern2");
				registerArbject("9e cc 40 d2", "sasha", "pattern4");
				registerArbject("ee 0a 6d b7", "shield", "pattern_shield");
				registerArbject("de cf 3d d2", "lens", "pattern_lens");
				// registerArbject("ee 42 39 d2", "harry", "pattern_harry");
				registerArbject("6c be 02 06 66 24 16 e0", "harry",
						"pattern_harry");
				// registerArbject("ae 67 3f d2", "tetris", "pattern_tetris");
				registerArbject("d6 db 86 30", "tetris", "pattern_tetris");
				registerArbject("ce 37 c6 aa", "axe", "pattern_axe");
				registerArbject("fe c3 c7 aa", "castle", "pattern_castle");
				registerArbject("6e b6 6e b7", "sword", "pattern_sword");
				registerArbject("ee aa 3d d2", "moon", "pattern_moon");
			}
		} catch (AndARException e) {
			e.printStackTrace();
		}
	}

	public void registerArbject(String arbjectID, String patternName,
			String patternFile) throws AndARException {
		arbjects.put(arbjectID, new PlanetArbject(patternName, patternFile,
				arbjectID, "", this));
		serverConnection.sendCommandToServer("currentValue:" + arbjectID + "."
				+ PLANET);
		artoolkit.registerARObject((ARObject) arbjects.get(arbjectID));
	}

	/**
	 * GAME LOOP
	 */
	public void update(float dt) {
		PlanetArbject planet = getPlanetForName("Luna");
		planet.update(dt);
	}

	public void serverUpdate() {
	}

	protected void onBeamComplete(NfcEvent event) {
	}

	/**
	 * BEAM EVENT
	 */
	protected void beamReceived(String deviceMac) {
	}

	/**
	 * TAG EVENT
	 */
	protected void nfcReceived(Arbject arbject) {
		currentArbject = (PlanetArbject) arbject;

		if (currentArbject.planet.name.equals("Terra")) {
			currentArbject.addFood(-100);
			currentArbject.addOxygen(-100);
			currentArbject.addEnergy(-1000);
			currentArbject.addPopulation(-1);
			food += 100;
			oxygen += 100;
			energy += 1000;
			population += 1;
		} else if (currentArbject.planet.name.equals("Luna")) {
			currentArbject.addFood(food);
			currentArbject.addOxygen(oxygen);
			currentArbject.addEnergy(energy);
			currentArbject.addPopulation(population);

			food = 0;
			oxygen = 0;
			energy = 0;
			population = 0;
		}
	}

	/**
	 * TOUCH EVENT
	 */
	public void performTapAction(String action, Object object) {
		if(action.equals(SETTINGS)){
			Intent intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
		}
	}

	/**
	 * ACCELEREOMETER
	 */
	protected void accelerometerChanged(float ax, float ay, float az) {
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * SERVER EVENT
	 */
	public void serverConnectionEventOccurred(String[] msg) {
		if (msg[1].equals(PLANET)) {
			((PlanetArbject) arbjects.get(msg[0])).updateForPack(msg[2]);
		}
	}

	public PlanetArbject getPlanetForName(String name) {
		PlanetArbject pArbject = null;
		for (Arbject arbject : arbjects.values()) {
			if (((PlanetArbject) arbject).planet.name.equals(name)) {
				pArbject = (PlanetArbject) arbject;
			}
		}
		return pArbject;
	}

	private String packResources() {
		return "F:" + food + ", O:" + oxygen + ", E:" + energy + ", P:"
				+ population;
	}
}
